#region Using

using System;
using System.Xml;
using System.IO;
using System.Globalization;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using BlogEngine.Core;

#endregion

namespace BlogEngine.Core.Providers
{
  /// <summary>
  /// A storage provider for BlogEngine that uses XML files.
  /// <remarks>
  /// To build another provider, you can just copy and modify
  /// this one. Then add it to the web.config's BlogEngine section.
  /// </remarks>
  /// </summary>
    public partial class AzureBlogProvider : BlogProvider
  {
        CategoryMethods CategoryMethods = new CategoryMethods();

    #region Categories

    /// <summary>
    /// Gets a Category based on a Guid
    /// </summary>
    /// <param name="id">The category's Guid.</param>
    /// <returns>A matching Category</returns>
    public override Category SelectCategory(Guid id)
    {
      List<Category> categories = Category.Categories;

      Category category = new Category();

      foreach (Category cat in categories)
      {
        if (cat.Id == id)
          category = cat;
      }
      category.MarkOld();
      return category;
    }

    /// <summary>
    /// Inserts a Category
    /// </summary>
    /// <param name="category">Must be a valid Category object.</param>
    public override void InsertCategory(Category category)
    {
        
        List<Category> categories = Category.Categories;
        categories.Add(category);
        //foreach (Category cat in categories)
        //{
        //    cat.MarkOld();
        //}

        string errMessage = null;
        CategoryMethods.Insert(category, out errMessage);
    }

    /// <summary>
    /// Updates a Category
    /// </summary>
    /// <param name="category">Must be a valid Category object.</param>
    public override void UpdateCategory(Category category)
    {
        List<Category> categories = Category.Categories;
        categories.Remove(category);
        categories.Add(category);

        string errMessage = null;
        CategoryMethods.Update (category, out errMessage);
    }

    /// <summary>
    /// Deletes a Category
    /// </summary>
    /// <param name="category">Must be a valid Category object.</param>
    public override void DeleteCategory(Category category)
    {
        List<Category> categories = Category.Categories;
        categories.Remove(category);

        //string errMessage = null;
        CategoryMethods.Delete(category.Id.ToString());
    }

    /// <summary>
    /// Fills an unsorted list of categories.
    /// </summary>
    /// <returns>A List&lt;Category&gt; of all Categories.</returns>
    public override List<Category> FillCategories()
    {
        string errMessage = null;
        List<Category> categories = CategoryMethods.GetCategories(null, out errMessage).ToList();
        foreach (Category cat in categories)
        {
            cat.MarkOld();
        }
        return categories;
    }

    #endregion

  }
}
